package com.orhon.smartcampus.modules.material.fetcher;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.dao.SInformationDao;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudent;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.student.mm.StudentMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

/**
 * -根据班级获取学生信息, 返回学生基础信息以及是否入住宿舍标记
 * 
 * @author ebusu
 */
@Component
public class DormitoryStudentsFetcher implements DataFetcher {

	@Autowired
	private EclassDao eclassDao;

	@Autowired
	private TokenService tokenService;

	@Autowired
	private SInformationDao studentDao;

	@Override
	@Transactional
	public R get(DataFetchingEnvironment env) throws Exception {

		HashMap<String, Object> condition = ObjectToMap.to(env.getArgument("condition"));

		if (condition.get("id") == null) {
			return R.error("班级ID不能为空.");
		}

		Integer classId = Integer.valueOf(condition.get("id").toString());

		String gender = env.getArgument("gender");

		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);

		String sql = "select t.id, t.student_name, t.sex, "
				+ " ifnull( (select 1 from dormitory_bed where department_id = " + classId
				+ " and students_id = t.id), 0) as bedflag" + " from student_information t "
				+ " where t.id in ( select student_id from student_eclass_records where go_eclass = " + classId + ")";
		if (!StringUtils.isEmpty(gender)) {
			sql += " and t.sex = '" + gender + "'";
		}
		SQLQuery sqlQuery = this.studentDao.getTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql);
		sqlQuery.setFirstResult(page.getPage());
		sqlQuery.setMaxResults(page.getLimit());
		List<Object[]> studentsList = sqlQuery.list();

		List result = new ArrayList();

		if (!CollectionUtils.isEmpty(studentsList)) {
			for (Object[] item : studentsList) {
//				Map map = new HashMap<>();
//
//				map.put("id", Long.valueOf((Integer) item[0]));
//				map.put("name", (String) item[1]);
//				map.put("gender", (String) item[2]);
				
				SInformation sInformation = new SInformation();
				sInformation.setId(Long.valueOf((Integer) item[0]));
				sInformation.setStudent_name((String) item[1]);
				sInformation.setSex((String) item[2]);
				sInformation.setIs_move_with(((BigInteger) item[3]).intValue());
				
				GQLStudent gqlStudent = StudentMapper.MAPPER.toStudent(sInformation);

				result.add(gqlStudent);
			}
		}

		Eclass eclassEntity = this.eclassDao.findById(Long.valueOf(classId));

		GQLEclass eclass = EclassMapper.MAPPER.toEclass(eclassEntity);
		
		Map resultMap = new HashMap();
		
		resultMap.put("id", eclass.getId());
		resultMap.put("name", eclass.getName());
		resultMap.put("students", result);

		return R.ok().put("data", resultMap);
	}
}
